package com.cf.game.im.handler.chat;

import com.cf.game.group.RelationService;
import com.cf.game.im.dao.UserImConfigMapper;
import com.cf.game.im.handler.AbsHandler;
import com.cf.game.im.handler.Handler;
import com.cf.game.im.model.ChatMsg;
import com.cf.game.im.model.UserImConfig;
import com.cf.game.msg.ChatMessageRequest;
import com.cf.mapper.BeanMapper;
import com.cf.redis.core.RedisPusher;
import com.cf.util.ObjectJsonUtil;
import io.vertx.core.eventbus.EventBus;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * boot-game-ChatHandler
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/8/1 上午 11:35
 */
@Slf4j
@Component
public class SingleChatHandler extends AbsHandler implements Handler {

    @Autowired
    private BeanMapper beanMapper;

    @Autowired
    private RedisPusher redisPusher;

    @Autowired
    private UserImConfigMapper userImConfigMapper;

    @Autowired
    private RelationService relationService;

    @Override
    public void handle(Map dataMap, EventBus eventBus) {
        ChatMessageRequest chatMsg = beanMapper.map(dataMap, ChatMessageRequest.class);
        if (StringUtils.isEmpty(chatMsg.getContent())) {
            log.info("收到的消息是空消息，忽略掉！");
            return;
        }
        handleSingle(chatMsg, eventBus);
    }


    public void handleSingle(ChatMessageRequest chatMsg, EventBus ebs) {


        //判断是否可以发送给此人
        boolean authResult = checkUserCanReceiveMessage(chatMsg);

        if (authResult) {

            ChatMsg cMsg = saveChatMessage(chatMsg);

            //分发消息
            deliveryMsg(chatMsg.getReceiver(), ebs, cMsg, chatMsg.getType());

            //客户端处理方式是下表显示已发送
            sendStepAck(chatMsg.getSender(), chatMsg.getUinId(), ebs);

            //发送处理的消息
            sendFollowMessage(chatMsg);

        } else {
            log.info("消息发送未被允许！");
        }
    }

    private void sendFollowMessage(ChatMessageRequest chatMsg) {
        Map extra = chatMsg.getExtra();
        if (extra != null) {
            Object follow = extra.get("follow");
            if (follow instanceof String) {
                String followTopic = (String) follow;
                if (StringUtils.isNotEmpty(followTopic)) {
                    redisPusher.convertAndSend(followTopic, ObjectJsonUtil.toString(chatMsg));
                }
            } else {
                log.info("chat消息中的extra消息无follow数据，不需要通知处理！【{}】", ObjectJsonUtil.toString(chatMsg));
            }
        } else {
            log.info("chat消息不存在extra，无需通知处理！【{}】", ObjectJsonUtil.toString(chatMsg));
        }
    }

    private boolean checkUserCanReceiveMessage(ChatMessageRequest chatMsg) {
        boolean isFriend = relationService.isFriend(chatMsg.getSender(), chatMsg.getReceiver());
        if (!isFriend) {
            UserImConfig userConfig = userImConfigMapper.selectByUserId(chatMsg.getReceiver());
            if (userConfig != null) {
                return userConfig.getReceiveTemporaryMsg();
            } else {
                return true;
            }
        }
        return true;
    }


    @Override
    public String getSupport() {
        return "singleChat";
    }
}
